package com.heima.test4;

import java.lang.reflect.Constructor;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

public class Test01 {
    public static void main(String[] args) {
        try {
            // - 通过反射获取构造方法,创建5个学生对象, 存到HashSet里
            HashSet<Student> set = new HashSet<>();
            Class<Student> stu = Student.class;
            Constructor<Student> con = stu.getConstructor(String.class, int.class);
            Student s1 = con.newInstance("张三", 15);
            Student s2 = con.newInstance("李四", 19);
            Student s3 = con.newInstance("王五", 20);
            Student s4 = con.newInstance("张三", 15);
            Student s5 = con.newInstance("赵六", 22);
            set.add(s1);
            set.add(s2);
            set.add(s3);
            set.add(s4);
            set.add(s5);
            // - 要求同样名字和年龄的为同一个学生,不被添加到集合中
            System.out.println(set);
            // - 过滤集合中所有年龄小于18的学生,收集到List集合中
            List<Student> list = set.stream().filter(student -> student.age < 18).collect(Collectors.toList());
            System.out.println(list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
